<template>
  <div class="home">
    <h1>{{ title }}</h1>
    <div class="box">
      <el-input v-model="val" placeholder="请输入内容"></el-input>
      <el-button @click="add(val)" type="primary">添加</el-button>
    </div>
    <todo-lists
      v-for="(item, index) in todos"
      :key="index"
      :item="item.content"
      :id="item.objectId"
    />
  </div>
</template>

<script>
// @ is an alias to /src
import TodoLists from "@/components/TodoLists";
import { getTodos, creatTodo } from "@/api";

export default {
  name: "Home",
  data() {
    return {
      title: process.env.NODE_ENV,
      todos: [],
      val: "",
    };
  },
  // created() {
  //   this.list()
  // },
  components: {
    TodoLists,
  },
  async created() {
    // 请求默认列表
    await this.list();
  },
  methods: {
    async list() {
      try {
        const res = await getTodos();
        const { data } = res.data;
        this.todos = data;
      } catch (e) {
        console.log(e);
      }
    },
    async add() {
      try {
        await creatTodo({
          content: this.val,
        });
        await this.list();
        this.val = "";
      } catch (e) {
        console.log(e);
      }
      console.log(this.val);
    },
  },
};
</script>

<style lang="less" scoped>
.box {
  display: flex;
  justify-content: center;
  width: 800px;
  margin: 0 auto;
  .input {
    width: 400px;
    height: 40px;
    font-size: 16px;
    padding-left: 10px;
  }
  .input:focus {
    outline: none;
  }
  .add {
    width: 60px;
    height: 46px;
  }
}
</style>
